package handler

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"new_house/api/request"
	"new_house/consts"
	"new_house/internal/dao"
	"new_house/internal/model"
	"new_house/internal/queue"
	"new_house/pkg"
	"path/filepath"
)

func AddHouse(c *gin.Context) {
	var req request.AddHouseReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(200, gin.H{
			"code":    10000,
			"message": "参数错误",
			"data":    nil,
		})
		return
	}
	var user model.User
	dao.GetOneById(c.GetUint("userId"), &user)
	if !(user.UType == 2 || user.UType == 3) {
		c.JSON(200, gin.H{
			"code":    10000,
			"message": "只有经纪人和管理员可以发布房源",
			"data":    nil,
		})
		return
	}
	if !TextCensor(req.Title, c.GetUint("userId"), "房源添加") {
		c.JSON(200, gin.H{
			"code":    10000,
			"message": "发布的内容中包含敏感词汇，禁止发布",
			"data":    nil,
		})
		return
	}
	result := dao.Create(&model.House{
		Title:    req.Title,
		Price:    req.Price,
		Area:     req.Area,
		City:     req.City,
		Address:  req.Address,
		Town:     req.Town,
		HouseImg: req.HouseImg,
		VideoUrl: req.VideoUrl,
		Status:   req.Status,
		UserId:   c.GetUint("userId"),
	})
	if !result {
		c.JSON(200, gin.H{
			"code":    10000,
			"message": "发布房源失败",
			"data":    false,
		})
		return
	}
	c.JSON(200, gin.H{
		"code":    http.StatusOK,
		"message": "发布房源成功",
		"data":    true,
	})
}

func TextCensor(content string, userId uint, bName string) bool {
	if pkg.TextCensor(content) {
		return true
	}
	dao.Create(&model.Safe{
		UserId:  userId,
		Content: content,
		BName:   bName,
	})
	return false
}

func GetHousePageList(c *gin.Context) {
	var req request.GetHousePageListReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(200, gin.H{
			"code":    10000,
			"message": "参数错误",
			"data":    nil,
		})
		return
	}
	pageDto := dao.PageDto{
		Page:     req.Page,
		PageSize: req.PageSize,
	}
	var houseList []model.House
	paginate, _ := dao.Paginate(&model.House{
		Id:    req.Id,
		Title: req.Title,
	}, pageDto, &houseList)
	c.JSON(200, gin.H{
		"code":    http.StatusOK,
		"message": "房源分页展示成功",
		"data":    paginate,
	})
}

func GetHouseDetail(c *gin.Context) {
	var req request.GetHouseDetailReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(200, gin.H{
			"code":    10000,
			"message": "参数错误",
			"data":    nil,
		})
		return
	}
	var house model.House
	dao.GetOneById(req.Id, &house)
	c.JSON(200, gin.H{
		"code":    http.StatusOK,
		"message": "房源详情展示成功",
		"data":    house,
	})
}

func Upload(c *gin.Context) {
	var user model.User
	dao.GetOneById(c.GetUint("userId"), &user)
	if !(user.UType == 2 || user.UType == 3) {
		c.JSON(200, gin.H{
			"code":    10000,
			"message": "该用户无权限上传房源照片",
			"data":    false,
		})
		return
	}
	file, err := c.FormFile("file")
	if err != nil {
		c.JSON(200, gin.H{
			"code":    10000,
			"message": "上传文件失败",
			"data":    nil,
		})
		return
	}
	dst := "D:\\gowork\\src\\new_house\\static\\upload\\" + file.Filename
	err = c.SaveUploadedFile(file, dst)
	if err != nil {
		c.JSON(200, gin.H{
			"code":    10000,
			"message": "上传文件失败",
			"data":    nil,
		})
		return
	}
	c.JSON(200, gin.H{
		"code":    http.StatusOK,
		"message": "上传文件成功",
		"data":    fmt.Sprintf("http://localhost:8080/upload/" + file.Filename),
	})
}

func UploadVideoUrl(c *gin.Context) {
	var user model.User
	dao.GetOneById(c.GetUint("userId"), &user)
	if !(user.UType == 2 || user.UType == 3) {
		c.JSON(200, gin.H{
			"code":    10000,
			"message": "该用户无权限上传视频",
			"data":    false,
		})
		return
	}
	file, err := c.FormFile("video")
	if err != nil {
		c.JSON(200, gin.H{
			"code":    10000,
			"message": "上传视频失败",
			"data":    nil,
		})
		return
	}
	dst := "D:\\gowork\\src\\new_house\\static\\upload\\" + file.Filename
	ext := filepath.Ext(file.Filename)
	if ext != ".mp4" {
		c.JSON(200, gin.H{
			"code":    10000,
			"message": "上传的文件格式不正确",
			"data":    nil,
		})
		return
	}
	if file.Size > 200*1024*1024 {
		c.JSON(200, gin.H{
			"code":    10000,
			"message": "上传的文件大小不可超过200MB",
			"data":    nil,
		})
		return
	}
	err = c.SaveUploadedFile(file, dst)
	if err != nil {
		c.JSON(200, gin.H{
			"code":    10000,
			"message": "上传文件失败",
			"data":    nil,
		})
		return
	}
	c.JSON(200, gin.H{
		"code":    http.StatusOK,
		"message": "上传文件成功",
		"data":    fmt.Sprintf("http://localhost:8080/upload/" + file.Filename),
	})
}

func UpdateHousePrice(c *gin.Context) {
	var req request.UpdateHousePriceReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(200, gin.H{
			"code":    10000,
			"message": "参数错误",
			"data":    nil,
		})
		return
	}
	var user model.User
	dao.GetOneById(c.GetUint("userId"), &user)
	if !(user.UType == 3) {
		c.JSON(200, gin.H{
			"code":    10000,
			"message": "只有管理员可以修改房源",
			"data":    nil,
		})
		return
	}
	var house model.House
	if req.Price > 0 && (req.Price != house.Price) {
		go queue.PublishPub(consts.WS_EX, &model.House{
			Id:    req.Id,
			Title: house.Title,
			Price: req.Price,
		})
	}
	result := dao.Update(&model.House{
		Id:       req.Id,
		Title:    req.Title,
		Price:    req.Price,
		Area:     req.Area,
		City:     req.City,
		Address:  req.Address,
		Town:     req.Town,
		HouseImg: req.HouseImg,
		VideoUrl: req.VideoUrl,
		Status:   req.Status,
		UserId:   c.GetUint("userId"),
	})
	if !result {
		c.JSON(200, gin.H{
			"code":    10000,
			"message": "修改房源失败",
			"data":    false,
		})
		return
	}
	c.JSON(200, gin.H{
		"code":    http.StatusOK,
		"message": "修改房源成功",
		"data":    true,
	})

}
